package com.sensePlusVR.activity.update;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.sensePlusVR.application.VRApplication;
import com.sensePlusVR.bean.AppVersionInfo;
import com.sensePlusVR.main.R;
import com.sensePlusVR.utils.AppManager;
import com.sensePlusVR.utils.Constant;
import com.sensePlusVR.utils.FileUtils;
import com.sensePlusVR.utils.MyLog;
import com.sensePlusVR.utils.SslHttps;
import com.sensePlusVR.utils.VersionUtil;
import com.sensePlusVR.widget.CircleDLProgressView;

import org.xutils.common.Callback;
import org.xutils.common.Callback.Cancelable;
import org.xutils.common.Callback.CancelledException;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;

public class AppProgressActivity extends Activity implements OnClickListener{
	
	private static final String TAG = AppProgressActivity.class.getSimpleName();
	
	private Button cancleBtn;
	
	private CircleDLProgressView circleView;
	
	private TextView versionTv;
	
	private AppVersionInfo newAppVersionInfo;
	
	private FileProgressCallback mFileProgressCallback;
	
	private Cancelable mCancelable;
	
	private static final long maxCount = 100;
	
	private String apkDir;
	
	private String apkPath;
	

	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		apkDir = getExternalCacheDir().getAbsolutePath()+Constant.PATH_APK_DIR;
		
		apkPath = getExternalCacheDir().getAbsolutePath()+Constant.PATH_APK;
		
		setContentView(R.layout.activity_app_progress);
		
		cancleBtn = (Button) findViewById(R.id.app_progress_cancel);
		
		circleView = (CircleDLProgressView) findViewById(R.id.app_progress_cicle);
		
		versionTv = (TextView) findViewById(R.id.app_progress_version);
		
		AppManager.getInstance().addActivity(this);
		
		
		
		//删除文件
		deleteApkFile();
		
		circleView.setMaxCount(maxCount);
		
		newAppVersionInfo = ((VRApplication)getApplication()).getNewVersionInfo();
		
		versionTv.setText(newAppVersionInfo.getVersonname());
		
		cancleBtn.setOnClickListener(this);
		
		mFileProgressCallback = new FileProgressCallback();
		
		String apkurl = newAppVersionInfo.getApkurl();
		MyLog.e(TAG, "apkurl : " + apkurl);
		
		RequestParams params = new RequestParams(apkurl);
		
		params.setAutoRename(false);
		if (apkurl.contains("https")) {
			MyLog.e(TAG, "use https to download...");
			params.setSslSocketFactory(SslHttps.getSSLContext(this).getSocketFactory());
		}
		params.setSaveFilePath(apkPath);
		
		
		mCancelable = x.http().get(params, mFileProgressCallback);
				
	}


	@Override
	public void onClick(View v) {
		
		switch(v.getId()){
		
		case R.id.app_progress_cancel:
			if(mCancelable!=null){
				
				mCancelable.cancel();
			}
			
			AppManager.getInstance().finishActivity(this);
			break;
		}
		
	}
	

	
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		mFileProgressCallback.setCallback(mDownCallback);
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
		mFileProgressCallback.setCallback(null);
		
		
	}
	
	private DownCallback mDownCallback = new DownCallback(){

		@Override
		public void onCancelled(CancelledException arg0) {
			
			
			
		}

		@Override
		public void onError(Throwable arg0, boolean arg1) {
			
			
		}

		@Override
		public void onFinished() {
			
			
			
		}

		@Override
		public void onSuccess(File arg0) {
			
			
		}

		@Override
		public void onLoading(long arg0, long arg1, boolean arg2) {
			
			MyLog.d(TAG, "DownCallback onLoading total:"+arg0+" current:"+arg1);
			
			int percent = (int) (arg1*1.0f/arg0*maxCount);
			
			circleView.setCurrentCount(percent);
			
			circleView.setScore(percent);
		}

		@Override
		public void onStarted() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onWaiting() {
			// TODO Auto-generated method stub
			
		}
		
	};
	
	
	
	
	//回调接口
	private interface DownCallback{
		
		
		public void onCancelled(CancelledException arg0);

	
		public void onError(Throwable arg0, boolean arg1);

		
		public void onFinished();
		
		public void onSuccess(File arg0);

		
		public void onLoading(long arg0, long arg1, boolean arg2);
		
		
		public void onStarted();

		
		public void onWaiting();
	}
	
	
	private class FileProgressCallback implements Callback.ProgressCallback<File>{
		
		private DownCallback downCallback;
		
		public void setCallback(DownCallback downCallback){
			
			this.downCallback = downCallback;
		}
		

		@Override
		public void onError(Throwable throwable, boolean flag) {
			
			Log.d(TAG, "FileProgressCallback onError :"+throwable);
			
			if(downCallback!=null){
				
				downCallback.onError(throwable, flag);
			}
			
			
			
		}

		@Override
		public void onCancelled(CancelledException cancelledexception) {
			
			Log.d(TAG, "FileProgressCallback oncalleled");
			
			if(downCallback!=null){
				
				downCallback.onCancelled(cancelledexception);
			}
			
			deleteApkFile();
		
		
		}

		@Override
		public void onFinished() {
			MyLog.d(TAG, "FileProgressCallback onFinished");
			
			if(downCallback!=null){
				
				downCallback.onFinished();
			}
			
			
		}

		@Override
		public void onSuccess(File arg0) {
			
			MyLog.d(TAG, "FileProgressCallback onSuccess");
			
			if(downCallback!=null){
				
				downCallback.onSuccess(arg0);
			}
			
			VersionUtil.installApk(AppProgressActivity.this, apkPath);
			
		}

		@Override
		public void onLoading(long arg0, long arg1, boolean arg2) {
			
			MyLog.d(TAG, "FileProgressCallback onLoading total:"+arg0+" current:"+arg1);
			
			if(downCallback!=null){
				
				downCallback.onLoading(arg0,arg1,arg2);
			}
			
			
		}

		@Override
		public void onStarted() {
			MyLog.d(TAG, "FileProgressCallback onStarted");
			
			if(downCallback!=null){
				
				downCallback.onStarted();
			}
			
		}

		@Override
		public void onWaiting() {
			
			MyLog.d(TAG, "FileProgressCallback onWaiting");
			
			if(downCallback!=null){
				
				downCallback.onWaiting();
			}
			
		}
		
	}
	

	/**
	 * 删除apk文件
	 */
	private void deleteApkFile(){
		

		FileUtils.deleteFile(apkDir);
		
	
		
		
	}
	
	@Override
	public void onBackPressed() {
		
		if(mCancelable!=null){
			
			mCancelable.cancel();
		}
		
		super.onBackPressed();
		

	}
	
	
	

}
